export default {
  /**
   * @description -- 自定义判断元素类型
   */
  toType(obj) {
    return Object.toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
  },

  /**
   * @description -- 滤参
   */
  filterNull(o) {
    let params = {}
    for (var key in o) {
      if (o[key] !== null && o[key] !== undefined) {
        if (this.toType(o[key]) === "string") {
          params[key] = o[key].trim()
        } else {
          params[key] = o[key]
        }
      }
    }
    return params
  },

  /**
   * @description -- 本地存储
   */
  storage: {
    session: {
      get(key) {
        return sessionStorage[key]
      },
      set(key, val, isCover) {
        if (sessionStorage[key] && !isCover) {
          alert("[" + key + "]已经存在")
        } else {
          if (sessionStorage[key]) {
            alert("[" + key + "]被覆盖")
          }
          sessionStorage[key] = val
        }
      }
    },
    local: {
      get(key) {
        return localStorage[key]
      },
      set(key, val, isCover) {
        if (localStorage[key] && !isCover) {
          alert("[" + key + "]已经存在")
        } else {
          if (localStorage[key]) {
            alert("[" + key + "]被覆盖")
          }
          localStorage[key] = val
        }
      }
    }
  },
  /**
   * @description -- 从url中解析参数
   * @param {String} url
   */
  getParams(url) {
    const keyValueArr = url.split("?")[1].split("&")
    let paramObj = {}
    keyValueArr.forEach(item => {
      const keyValue = item.split("=")
      paramObj[keyValue[0]] = keyValue[1]
    })
    return paramObj
  }
}
